package cn.tiansu.eway.kylinengine.manager;

import java.util.Date;

import org.apache.log4j.Logger;

import cn.tiansu.eway.common.utils.Log4jInitializer;
import cn.tiansu.eway.common.utils.PropertyConfigurator;

/**
 * kylin olap 入口程序
 * 
 * @Title: KYLINManager.java
 * @Description: TODO
 * @author: 吴鹏
 * @date: 2015年9月16日
 * 
 *
 */
public class KYLINManager
{
	// static {
	// System.setProperty("log4j.configurationFile", "config/log4j2.xml");
	// }

	/**
	 * log
	 */
	private static final Logger log = Logger.getLogger(KYLINManager.class);

	/**
	 * 模块名称
	 */
	public final static String MODELNAME = "OLAPKylinEngine";

	public final static String Response = "OLAPKylinEngineResponce";

	public final static String ResponseCmd = "OLAPKylinEngineResponce_cmd";

	/**
	 * 命令
	 */
	public final static String CMD = "kylin";

	/**
	 * 租户销户注册topic
	 */
	public final static String tenantUpdate = "PubTenantUpdated";

	public static void main(String[] args)
	{
		PropertyConfigurator config = PropertyConfigurator.getInstance(null,
				"config.properties");

		Log4jInitializer.init(null);

		String verion = config.getParameter("verion");
		String model = config.getParameter("model");

		if (args == null || args.length == 0 || args.length == 1)
		{
			System.out.println(args.length);
			System.out.println("Options:");
			System.out.println("-v,  --version   版本信息");
			System.out.println("-h,  --help      帮助");
			System.out.println("10 12,  --指定小时和分钟执行，只有当10点12分时才会执行");
			return;
		}

		if (args[0].equals("-v") || args[0].equals("-version"))
		{
			System.out.println(verion);
			System.out.println(model);

			return;
		} else if (args[0].equals("-h") || args[0].equals("-help"))
		{
			System.out.println("Options:");
			System.out.println("-v,  --version   版本信息");
			System.out.println("-h,  --help      帮助");
			return;
		}

		// 指定分钟
		int hour = Integer.valueOf(args[0]);
		int minutes = Integer.valueOf(args[1]);
		
		Date date = new Date();
		// 当前分钟
		int currHour = date.getHours();
		int currMin = date.getMinutes();

		log.info(verion);
		log.info(model);

		KYLINEngineListener l = new KYLINEngineListener();
		l.init();
		
		// // 初始化kafka，注册模块信息
		String privateName = config.getParameter("privateName");

		// 周期性发送运行状态
		l.sendPubModuleStatus(MODELNAME, privateName);

		// 当前10分钟，指定15分钟
		if (minutes - currMin >= 0)
		{
			try
			{
				long sec = (minutes - currMin) * 60000l+hour*3600000l;
				log.info("sec="+sec);
				Thread.sleep(sec);
			} catch (InterruptedException e)
			{
				e.printStackTrace();
			}
		}
		// 当前15分钟，指定10分钟
		else
		{
			try
			{
				long sec = (60 - (currMin - minutes)) * 60000l+hour*3600000l;
				log.info("sec="+sec);
				Thread.sleep(sec);
			} catch (InterruptedException e)
			{
				e.printStackTrace();
			}
		}

		// 周期性的构建立方
		l.buildCubePeriod();

	}

}
